﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using log4net;

namespace $UIProjectName$
{
    public class Global : System.Web.HttpApplication, IContainerAccessor
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Global));
        private static IWindsorContainer _container;

        /// <summary>
        /// Gets the IoC container
        /// </summary>
        public IWindsorContainer Container
        {
            get
            {
                return _container;
            }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            log.Info("Application started");

            // ----------------------------
            // IoC container initialisation
            // ----------------------------
            _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));

            log.Info("Ioc container initialized");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            // Logs the error
            log.Error("Application Error", Server.GetLastError());
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            _container.Dispose();
        }
    }
}